package com.cskaoyan.shopping.service.impl;

import com.cskaoyan.mall.commons.exception.ExceptionProcessorUtils;
import com.cskaoyan.mall.constant.ShoppingRetCode;
import com.cskaoyan.mall.dto.ProductDetailDto;
import com.cskaoyan.mall.dto.ProductDetailRequest;
import com.cskaoyan.mall.dto.ProductDetailResponse;
import com.cskaoyan.mall.dto.ProductDto;
import com.cskaoyan.shopping.converter.ProductConverter;
import com.cskaoyan.shopping.converter.ProductDetailConverter;
import com.cskaoyan.shopping.dal.entitys.Item;
import com.cskaoyan.shopping.dal.entitys.ItemDesc;
import com.cskaoyan.shopping.dal.persistence.ItemDescMapper;
import com.cskaoyan.shopping.dal.persistence.ItemMapper;
import com.cskaoyan.shopping.dto.AllProductRequest;
import com.cskaoyan.shopping.dto.AllProductResponse;
import com.cskaoyan.shopping.service.IDetailService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * @author :rain
 * @description
 * @since 2022/07/09 10:28
 */
@Slf4j
@Service
public class IDetailServiceImpl implements IDetailService {
    @Autowired
    ItemMapper itemMapper;

    @Autowired
    ItemDescMapper itemDescMapper;

    @Autowired
    ProductDetailConverter converter;

    @Autowired
    ProductConverter productConverter;

    /**
     * 获取商品详情页面
     *
     * @param request
     * @return
     */
    @Override
    public ProductDetailResponse getProductDetail(ProductDetailRequest request) {
        ProductDetailResponse response = new ProductDetailResponse();
        try {
            // 参数校验
            request.requestCheck();
            Item item = itemMapper.selectByPrimaryKey(request.getId());
            //这里可以用typeHandler
            String[] images = item.getImages();
            //转换成Dto
            ProductDetailDto productDetail = converter.itemToProductDetailDto(item);
            //设置当前的显示图片，显示数组中的第一个图片
            productDetail.setProductImageBig(item.getImageBig());
            //设置当前所有图片显示
            productDetail.setProductImageSmall(Arrays.asList(images));
            ItemDesc itemDesc = itemDescMapper.selectByPrimaryKey(request.getId());
            //封装规格的图片
            productDetail.setDetail(itemDesc.getItemDesc());
            //执行成功
            response.setCode(ShoppingRetCode.SUCCESS.getCode());
            response.setMsg(ShoppingRetCode.SUCCESS.getMessage());
            response.setProductDetailDto(productDetail);
        } catch (Exception e) {
            //e.printStackTrace();
            log.error("IDetailServiceImpl.getProductDetail occur Exception :" + e);
            ExceptionProcessorUtils.wrapperHandlerException(response, e);
        }
        return response;
    }

    /**
     * 分页获取所有商品的信息
     *
     * @param request
     * @return
     */
    @Override
    public AllProductResponse getAllProduct(AllProductRequest request) {
        //开启分页
        PageHelper.startPage(request.getPage(), request.getSize());
        //返回值对象
        AllProductResponse response = new AllProductResponse();
        try {
            //参数校验
            request.requestCheck();
            List<Item> items;
            //判断。如果sort为null，默认按照根据商品创建时间created字段排序，排序方式为降序
            if (request.getSort() == null || request.getSort().equals("")) {
                items = itemMapper.selectItemFront(request.getCid(), "created", "desc",
                        request.getPriceGt(), request.getPriceLte());

                //按照价格排序，1表示升序
            } else if (request.getSort().equals("1")) {
                items = itemMapper.selectItemFront(request.getCid(), "price", "asc",
                        request.getPriceGt(), request.getPriceLte());

                //按照价格排序，-1表是降序
            } else {
                items = itemMapper.selectItemFront(request.getCid(), "price", "desc",
                        request.getPriceGt(), request.getPriceLte());
            }
            //转换成Dto
            List<ProductDto> productDtos = productConverter.items2Dto(items);
            //执行成功
            response.setCode(ShoppingRetCode.SUCCESS.getCode());
            response.setMsg(ShoppingRetCode.SUCCESS.getMessage());
            response.setData(productDtos);
            //Long total = Long.valueOf((productDtos.size()));
            //对查询结果进行分页
            PageInfo<Item> pageInfo = new PageInfo<>(items);
            //获取分页之后的total(分页之后的页面)
            response.setTotal(pageInfo.getTotal());
        } catch (Exception e) {
            //e.printStackTrace();
            log.error("IDetailServiceImpl.getAllProduct occur Exception :" + e);
            ExceptionProcessorUtils.wrapperHandlerException(response, e);
        }

        return response;
    }


}
